'''
作业
'''
from random import randint


# 1
class Computer:
    def __init__(self, brand, color='black', size='16G'):
        self.brand = brand
        self.color = color
        self.size = size

    def play(self):
        print('play')

    def coding(self):
        print('coding')

    def mv(self):
        print('mv')


# a.
c1 = Computer('华为')
print(c1.brand)
c1.brand = 'apple'
c1.screen = '14'
del c1.screen
# b.
getattr(c1, 'brand')
setattr(c1, 'size', '32G')
setattr(c1, 'screen', '14')
delattr(c1, 'screen')


# 2.
class Person:
    def __init__(self, name, age, dog=None):
        self.name = name
        self.age = age
        self.dog = dog

    def dogging(self):
        if self.dog:
            print('遛狗：', self.dog.name)
        else:
            print('没有狗')


class Dog:
    def __init__(self, name, color, age):
        self.name = name
        self.age = age
        self.color = color

    def bark(self):
        print('%s叫唤' % self.name)


p1 = Person('小明', '20')
p1.dog = Dog('大黄', '黄色', 2)
p1.dogging()


# 3.
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance(self, other):
        """
        求两点之间距离
        :param other:另一个点对象
        :return: 距离
        """
        a = self.x - other.x
        b = self.y - other.y
        return (a ** 2 + b ** 2) ** 0.5


class Circle:
    pi = 3.1415926

    def __init__(self, r, center: Point):
        self.r = r
        self.center = center

    def area(self):
        print('圆的面积：', self.pi * self.r ** 2)

    def circle_distance(self, other):
        return self.center.distance(other.center)


class Line:
    def __init__(self, s: Point, e: Point):
        self.s = s
        self.e = e

    def length(self):
        return self.s.distance(self.e)


# c1 = Circle(2)
# c1.area()


# 4.
class Student:

    def __init__(self, name, age, sid):
        self.name = name
        self.age = age
        self.sid = sid
        self.status = randint(0, 1)

    def answer(self):
        if self.status:
            print('答到：', self.name)

    def msg(self):
        print('学生信息：', self.name, self.age, self.sid)


"""
class_name = [
    {'sname':sname,'age':age,'sid':sid}
]
"""


class Clazz:

    def __init__(self, name: str):
        self.name = name
        self.stus = []

    def add(self, stu):
        self.stus.append(stu)

    def _delete(self, stu):
        self.stus.remove(stu)

    def check(self):
        for stu in self.stus:
            print(stu.name)
            stu.answer()

    def avg(self):
        sum1 = 0
        for stu in self.stus:
            sum1 += stu.age
        avg = sum1 / len(self.stus)
        return avg

    def sort_age(self):
        self.stus.sort(key=lambda stu: stu.age)

    def show_all(self):
        for stu in self.stus:
            stu.msg()


s1 = Student('1', 20, '001')
s2 = Student('2', 28, '002')
c1 = Clazz('c1')
c1.add(s1)
c1.add(s2)
print(c1.stus)
print(c1.avg())
